package com.inspinia.base.api.controller;

import com.inspinia.base.api.model.ExternalInterface;
import com.inspinia.base.api.service.ExternalInterfaceService;
import com.inspinia.base.common.model.AjaxResult;
import com.inspinia.base.common.model.ResourcesType;
import com.inspinia.base.common.model.RolesEnum;
import com.inspinia.base.common.model.SubmenuOrResource;
import com.inspinia.base.pagination.Pager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 的Controller
 * Created by pujw on 2017-6-30.
 */
@Controller
@RequestMapping("apiManager/externalInterface")
public class ExternalInterfaceController {

    @Resource
    private ExternalInterfaceService externalInterfaceService;

    /**
     * 跳转分页页面
     *
     * @return
     */
    @SubmenuOrResource(name = "跳转分页页面", roles = RolesEnum.vosInterfaceManager, type = ResourcesType.resources)
    @RequestMapping(value = "paging", method = RequestMethod.GET)
    public String toPaginationPage() {
        return "externalInterface/externalInterface-pagination";
    }

    /**
     * 分页查询
     *
     * @return
     */
    @SubmenuOrResource(name = "分页查询", roles = RolesEnum.vosInterfaceManager, type = ResourcesType.resources)
    @ResponseBody
    @RequestMapping(value = "paging", method = RequestMethod.POST)
    public AjaxResult paging(ExternalInterface externalInterfaceVo) {
        Pager<ExternalInterface> page = externalInterfaceService.findPage(externalInterfaceVo);
        return new AjaxResult(true, page);
    }

    /**
     * 创建
     *
     * @param externalInterface
     * @return
     */
    @SubmenuOrResource(name = "创建", roles = RolesEnum.vosInterfaceManager, type = ResourcesType.resources)
    @ResponseBody
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public AjaxResult create(@Valid ExternalInterface externalInterface) {
        if (!externalInterface.getAddress().startsWith("/api/externalInterface/")) {
            return new AjaxResult(false, "接口路径必须以/api/externalInterface/开头!");
        }
        externalInterfaceService.insert(externalInterface);
        return new AjaxResult(true);
    }

    /**
     * 编辑
     *
     * @param externalInterface
     * @return
     */
    @SubmenuOrResource(name = "跳转编辑页面", roles = RolesEnum.vosInterfaceManager, type = ResourcesType.resources)
    @ResponseBody
    @RequestMapping(value = "goEdit", method = RequestMethod.POST)
    public AjaxResult goEdit(@Valid ExternalInterface externalInterface) {
        externalInterfaceService.update(externalInterface);
        return new AjaxResult(true);
    }

    /**
     * 编辑
     *
     * @param externalInterface
     * @return
     */
    @SubmenuOrResource(name = "编辑", roles = RolesEnum.vosInterfaceManager, type = ResourcesType.resources)
    @ResponseBody
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public AjaxResult edit(@Valid ExternalInterface externalInterface) {
        externalInterfaceService.update(externalInterface);
        return new AjaxResult(true);
    }

    /**
     * 根据id获取
     *
     * @param id id
     * @return
     */
    @SubmenuOrResource(name = "根据id获取", roles = RolesEnum.vosInterfaceManager, type = ResourcesType.resources)
    @ResponseBody
    @RequestMapping(value = "get", method = RequestMethod.GET)
    public AjaxResult get(long id) {
        ExternalInterface bean = externalInterfaceService.get(id);
        return new AjaxResult(true, bean);
    }

    /**
     * 禁用接口
     *
     * @param id 用户id
     * @return
     */
    @ResponseBody
    @RequestMapping("disable")
    @SubmenuOrResource(name = "禁用接口", roles = RolesEnum.vosInterfaceManager, type = ResourcesType.resources)
    public AjaxResult disable(Long id) {
        int count = externalInterfaceService.batchDisable(id);
        if (count > 0) {
            //todo 禁用接口
            return new AjaxResult(true);
        } else {
            return new AjaxResult(false, "禁用失败");
        }
    }

    /**
     * 启用接口
     *
     * @param id 用户id
     * @return
     */
    @ResponseBody
    @RequestMapping("enable")
    @SubmenuOrResource(name = "启用接口", roles = RolesEnum.vosInterfaceManager, type = ResourcesType.resources)
    public AjaxResult enable(Long id) {
        int count = externalInterfaceService.batchEnable(id);
        if (count > 0) {
            //todo 删除redis接口
            return new AjaxResult(true);
        } else {
            return new AjaxResult(false, "启用失败");
        }
    }

    /**
     * 修改接口激活状态
     *
     * @param interfaceId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateInterfaceActiveStatus", method = RequestMethod.POST)
    public AjaxResult updateInterfaceActiveStatus(Long interfaceId) {
        if (interfaceId == null) {
            return new AjaxResult(false, "id不能为空");
        }
        ExternalInterface externalInterface = externalInterfaceService.get(interfaceId);
        if (externalInterface.isEnable()) {
            externalInterface.setEnable(false);
        } else {
            externalInterface.setEnable(true);
        }
        int update = externalInterfaceService.update(externalInterface);
        if (update > 0) {
            return new AjaxResult(true);
        }
        return new AjaxResult(false, "更新失败");
    }


}